package com.weiyuwqj.admin.controller.admin.reward;

import com.alibaba.druid.util.StringUtils;
import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.BasCouponService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasCoupon;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/8/9.
 */
@Controller
@RequestMapping("/admin/coupon")
public class CouponController {

    @Autowired
    BasCouponService couponService;

    @Autowired
    BasUserService userService;

    @ResponseBody
    @RequestMapping(value="/list", method= RequestMethod.POST)
    public Object getList(Paginator p,String username,String beginDate,String endDate,Date useTime,String couponStatus,String id,String mobile){
        Map map=new HashMap();
        map.put("itemId",id);
        map.put("username",username);
        map.put("mobile",mobile);
        map.put("beginDate",beginDate);
        map.put("endDate",endDate);
        map.put("couponStatus",couponStatus);
        if(useTime!=null){
            map.put("useBeginTime", DateUtils.getDayStartTime(useTime));
            map.put("useEndTime",DateUtils.getNextDayStartTime(useTime));
        }
        return couponService.getList(p,map);
    }

    //获取单个红包信息
    @ResponseBody
    @RequestMapping("/getCouponByCouponId")
    public Object getCouponByCouponId(Integer couponId){
        return couponService.getSingle(couponId);
    }

    //添加红包
    @ResponseBody
    @RequestMapping("/add")
    @LogAnnotation(module ="奖励管理",description = "添加红包")
    public Object add(String username,BasCoupon coupon,String expiredTime){
        BasUser user=userService.getUserByUsername(username);
        if(user == null){
            throw new CrowdException("user.login.username.no_such_user");
        }
        coupon.setUserId(user.getId());
        coupon.setAddtime(new Date());
        coupon.setAddip(IpUtils.get());
        coupon.setDebtInvestId(0);
        coupon.setCouponName("系统奖励");
        if(coupon.getInvestId()==null){
            coupon.setInvestId(0);
        }
        if(coupon.getRuleId()==null){
            coupon.setRuleId(0);
        }

        if(StringUtils.isEmpty(expiredTime)){
            coupon.setExpiredTime(DateUtils.setDayLast(DateUtils.addDays(new Date(), 30))); //有效时间
        }else {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(expiredTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            coupon.setExpiredTime(DateUtils.setDayLast(date));
        }

        coupon.setStatus(2);
        couponService.save(coupon);
        return Message.OK;
    }
}
